<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 药品管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>新增药品</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="form-box">
                <el-form ref="formRef" :rules="rules" :model="form" label-width="80px">
                    <el-form-item label="药品名称" prop="medicineName">
                        <el-input v-model="form.medicineName"></el-input>
                    </el-form-item>
                    <el-form-item label="药品库存" prop="medicineQuantity">
                        <el-input v-model="form.medicineQuantity" type="number"></el-input>
                    </el-form-item>
                    <el-form-item label="药品售价" prop="medicinePrice">
                        <el-input v-model="form.medicinePrice" type="number"></el-input>
                    </el-form-item>
                    <el-form-item label="药品简介" prop="medicineIntro">
                        <el-input type="textarea" rows="5" v-model="form.medicineIntro"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">新增药品</el-button>
                        <el-button @click="onReset">清空表单</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { addMedicine } from "../../api";
export default {
    name: "medicineForm",
    setup() {
        const rules = {
            medicineName: [
                { required: true, message: "请输入药品名称", trigger: "blur" },
            ],
            medicineIntro: [
                { required: true, message: "请输入药品简介", trigger: "blur" },
            ],
            medicineQuantity: [
                { required: true, message: "请输入药品库存", trigger: "blur" },
                // { type: 'number', message: '请输入合法的数字', trigger: "blur"  },
            ],
            medicinePrice: [
                { required: true, message: "请输入药品售价", trigger: "blur" },
                // { type: 'number', message: '请输入合法的数字', trigger: "blur"  },
            ],
        };
        const formRef = ref(null);

        // 表单数据
        let form = reactive({
            medicineName: '',
            medicineIntro: '',
            medicineQuantity: null,
            medicinePrice: null,
        });




        // 重置
        const onReset = () => {
            formRef.value.resetFields();
        };

        // 提交
        const onSubmit = () => {
            // 表单校验
            formRef.value.validate((valid) => {
                if (valid) {
                    console.log(form);
                    addMedicine(form).then(res => {
                        if (res.code === 200) {
                            // reload();
                            onReset();
                            ElMessage.success('添加成功');
                        } else {
                            console.log("[添加药品] 失败: " + res.message);
                            ElMessage.error('服务器繁忙，请稍后重试');
                        }
                    })
                } else {
                    return false;
                }
            });
        };

        return {
            rules,
            formRef,
            form,
            onSubmit,
            onReset,
            
        };
    },
};
</script>